package com.sunq.studio.sys.controller;

import com.sunq.studio.resource.domain.TbPicResource;
import com.sunq.studio.resource.service.ResourceService;
import com.sunq.studio.sys.domain.TbClass;
import com.sunq.studio.sys.domain.TbSignClass;
import com.sunq.studio.sys.domain.TbSignTeacher;
import com.sunq.studio.sys.service.ClassService;
import com.sunq.studio.sys.vo.ClassSignVO;
import com.sunq.studio.utils.JsonToOtherUtil;
import com.sunq.studio.utils.OtherPojo;
import com.sunq.studio.utils.ResultObjVO;
import com.sunq.studio.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by sunq on 2019/7/3.
 */
@RestController
@RequestMapping("/class")
public class ClassController {


    @Autowired
    private ClassService classService;

    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/getClassList")
    public ResultObjVO getClassList(@RequestBody String data ){

        List<TbClass> res=classService.getClassAll();
        if(res==null){
            return ResultUtil.error("500","");
        }else{
            return  ResultUtil.success(res);
        }

    }
    @RequestMapping("/putSignClass")
    public ResultObjVO signClass(@RequestBody String data ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignClass() );
        TbSignClass singClass= (TbSignClass)ob.getObj();

        int i=classService.putSignClass(singClass);
        if(i>0){
            return ResultUtil.success("");
        }else{
            return  ResultUtil.error("500","扣款失败");
        }


    }
    @RequestMapping("/putTeachSignClass")
    public ResultObjVO putTeachSignClass(@RequestBody String data ){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignTeacher() );
        TbSignTeacher singClass= (TbSignTeacher)ob.getObj();

        int i=classService.putTeachSignClass(singClass);
        if(i>0){
            return ResultUtil.success("");
        }else{
            return  ResultUtil.error("500","扣款失败");
        }


    }

    @RequestMapping("/getTeacheAuditClass")
    public ResultObjVO getTeacheAuditClass(@RequestBody String data ){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignTeacher() );
        TbSignTeacher singClass= (TbSignTeacher)ob.getObj();

        if(singClass!=null){
            List<ClassSignVO> res=null;
            if(singClass.getAuditFlag()==0){
                res= classService.getUnauditList(ob.getPage() );
            }else{
                res= classService.getAuditedList(ob.getPage() );
            }

            if(res!=null){
                return ResultUtil.success(res);
            }else{
                return  ResultUtil.error("201","无数据");
            }

        }else{
            return  ResultUtil.error("501","查询错误");
        }


    }

    @RequestMapping("/setTeachClassAudit")
    public  ResultObjVO setTeachClassAudit(@RequestBody String data){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignTeacher() );
        TbSignTeacher singClass= (TbSignTeacher)ob.getObj();

        int res=classService.setTeachClassAudit(singClass);

        if(res==1){
            return ResultUtil.success(res);
        }else{
            return ResultUtil.error("500","系统异常");
        }
    }

    @RequestMapping("/getAuditClass")
    public  ResultObjVO getUnAuditClassSign(@RequestBody String data){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignClass() );
        TbSignClass singClass= (TbSignClass)ob.getObj();

        List<ClassSignVO> res=classService.getSignedClassList(singClass.getClassTime(),singClass.getAuditFlag(),ob.getPage());

        if(res!=null){
            return ResultUtil.success(res);
        }else{
            return ResultUtil.error("500","无签到数据");
        }


    }
    @RequestMapping("/setClassAuditPass")
    public  ResultObjVO setClassAuditPass(@RequestBody String data){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignClass() );
        TbSignClass singClass= (TbSignClass)ob.getObj();


        int res=classService.setClassAuditPass(singClass);

        if(res==1){
            return ResultUtil.success(res);
        }else{
            return ResultUtil.error("500","系统异常");
        }
    }

    @RequestMapping("/setClassAuditFail")
    public  ResultObjVO setClassAuditFail(@RequestBody String data){

        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignClass() );
        TbSignClass singClass= (TbSignClass)ob.getObj();
        int res=classService.setClassAuditDel(singClass);

        if(res==1){
            return ResultUtil.success(res);
        }else{
            return ResultUtil.error("500","系统异常");
        }
    }


    @RequestMapping("/getUserClassSign")
    public ResultObjVO getUserClassSign(@RequestBody String  data){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbSignClass() );
        TbSignClass singClass= (TbSignClass)ob.getObj();
        List<ClassSignVO> res =classService.getUserClassSign(singClass.getUserId(),ob.getPage());

        if(res==null){
            return ResultUtil.error("500","未能获取数据");
        }else{
            return ResultUtil.success(res);
        }

    }

    @RequestMapping("/addClass")
    public ResultObjVO addClass(@RequestBody String  data){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbClass() );
        TbClass tbClass= (TbClass)ob.getObj();
        Integer resId=0;
        int res=0;
        if(tbClass.getId()==null||tbClass.getId()==0){
            resId =classService.addClass(tbClass);
            res=1;
        }else{
            resId=tbClass.getId();
            res =classService.updateClass(tbClass);
        }

        if(!tbClass.getImages().isEmpty()){
            for(TbPicResource resPic:tbClass.getImages() ){
                if(resPic.getId()==null||resPic.getId()==0){
                    resourceService.addPicResource(resPic.getPicUrl() ,resId);
                }

            }
        }

        if(res==1){
            return ResultUtil.success("ok");
        }else{
            return ResultUtil.error("500","课程添加失败");
        }


    }
    @RequestMapping("/delClass")
    public ResultObjVO delClass(@RequestBody String  data){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbClass() );
        TbClass tbClass= (TbClass)ob.getObj();
        tbClass.setDelFlag(new Integer(1));
        int res =classService.updateClass(tbClass);
        if(res==1){
            return ResultUtil.success("ok");
        }else{
            return ResultUtil.error("500","课程添加失败");
        }
    }
    @RequestMapping("/getClassbyId")
    public ResultObjVO getClassbyId(@RequestBody String  data){
        OtherPojo ob = JsonToOtherUtil.rtOtherPojo(data, new TbClass() );
        TbClass tbClass= (TbClass)ob.getObj();
        TbClass res =classService.getClassbyId(tbClass.getId());
        if(res!=null){
            return ResultUtil.success(res);
        }else{
            return ResultUtil.error("500","课程查询失败");
        }
    }

}
